﻿using System;
using System.Collections.Generic;
using System.IO;
using com.echonest.api.v4;
using jENIKVMApi;

namespace Examples
{
    public class ArtistExamples
    {

        private EchoNestAPI en;
        private static bool trace = true;

        public ArtistExamples()
        {
            en = new EchoNestAPI(File.ReadAllText("developer_key.txt"));

            en.setTraceSends(trace);
            en.setTraceRecvs(false);
            en.setMinCommandTime(0);
        }

        public void dumpArtist(Artist artist)
        {
            Console.Write("{0}\n", artist.getName());
            Console.Write("   hottt {0:F3}\n", artist.getHotttnesss());
            Console.Write("   fam   {0:F3}\n", artist.getFamiliarity());

            Console.WriteLine(" =========  urls ======== ");
            foreach (string key in artist.GetUrls().Keys)
            {
                Console.Write("   {0} {1}\n", key, artist.GetUrls()[key]);
            }

            Console.WriteLine(" =========  audio ======== ");
            List<Audio> audioList = artist.GetAudio();
            for (int i = 0; i < audioList.Count; i++)
            {
                Console.Write("     == audio {0} == \n", i + 1);
                Audio audio = audioList[i];
                //audio.dump();
            }

            Console.WriteLine(" =========  audio2 ======== ");
            List<Audio> audioList2 = artist.GetAudio(10, 40);
            for (int i = 0; i < audioList2.Count; i++)
            {
                Audio audio = audioList2[i];
                //audio.dump();
            }

            Console.WriteLine(" =========  bios ======== ");
            List<Biography> bios = artist.GetBiographies();
            for (int i = 0; i < bios.Count; i++)
            {
                Biography bio = bios[i];
                bio.dump();
            }

            Console.WriteLine(" =========  blogs ======== ");
            List<Blog> blogs = artist.GetBlogs();
            for (int i = 0; i < blogs.Count; i++)
            {
                Blog blog = blogs[i];
                blog.dump();
            }

            Console.WriteLine(" =========  images ======== ");
            List<Image> images = artist.GetImages();
            for (int i = 0; i < images.Count; i++)
            {
                Image image = images[i];
                image.dump();
            }

            Console.WriteLine(" =========  news ======== ");
            List<News> newsList = artist.GetNews();
            for (int i = 0; i < newsList.Count; i++)
            {
                News news = newsList[i];
                news.dump();
            }

            Console.WriteLine(" =========  reviews ======== ");
            List<Review> reviews = artist.GetReviews();
            for (int i = 0; i < reviews.Count; i++)
            {
                Review review = reviews[i];
                review.dump();
            }

            Console.WriteLine(" =========  videos ======== ");
            List<Video> videos = artist.GetVideos();
            for (int i = 0; i < videos.Count; i++)
            {
                Video video = videos[i];
                video.dump
                ();
            }
        }

        public void searchArtistByName(string name, int results)
        {
            ArtistParams p = new  ArtistParams();
            p.setName(name);
            p.setResults(results);

            List<Artist> artists = en.SearchArtists(p);
            foreach (Artist artist in artists)
            {
                dumpArtist(artist);
                Console.WriteLine();
            }
        }

        public void randomWalk(string seedName, int count)
        {
            List<Artist> artists = en.SearchArtists(seedName);
            if (artists.Count > 0)
            {
                Artist seed = artists[0];
                for (int i = 0; i < count; i++)
                {
                    dumpArtist(seed);
                    List<Artist> sims = seed.GetSimilar(10);
                    if (sims.Count > 0)
                    {
                        Wintellect.PowerCollections.Algorithms.RandomShuffleInPlace(sims);
                        seed = sims[0];
                    }
                    else
                    {
                        break;
                    }
                }
            }
        }

        public void stats()
        {
            en.showStats();
        }

        public static void Run()
        {
            ArtistExamples sse = new ArtistExamples();
            long start = DateTime.Now.Millisecond;
            try
            {
                sse.searchArtistByName("weezer", 10);
                Console.WriteLine("Random walk");
                sse.randomWalk("weezer", 10);
            }
            finally
            {
                Console.WriteLine("Runtime " + (DateTime.Now.Millisecond - start));
                sse.stats();
            }
        }
    }

}